package com.school.filesearch110;

import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;

import br.com.mauker.materialsearchview.MaterialSearchView;
import cn.bmob.v3.Bmob;

public class MainActivity extends AppCompatActivity {

    private MaterialSearchView searchView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bmob.initialize(this, "751c63a2253c759e225b609d0353f344");
        setContentView(R.layout.activity_main);
        Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        searchView=(MaterialSearchView)findViewById(R.id.search_view);
        searchView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // Do something when the suggestion list is clicked.
                String suggestion = searchView.getSuggestionAtPosition(position);
                searchView.closeSearch();
                SearchActivity.actionStart(MainActivity.this,suggestion);
            }
        });
        searchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener(){
            @Override
            public boolean onQueryTextSubmit(String query)
            {
                String temp=searchView.getCurrentQuery();
                if(!temp.equals(""))
                {
                    searchView.closeSearch();
                    SearchActivity.actionStart(MainActivity.this,temp);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            @Override
            public boolean onQueryTextChange(String newText)
            {
                return false;
            }
        });
    }
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.toolbarbutton,menu);
        return true;
    }
    @Override
    public void onBackPressed() {
        if (searchView.isOpen()) {
            // Close the search on the back button press.
            searchView.closeSearch();
        } else {
            super.onBackPressed();
        }
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId()){
            case R.id.search:
                searchView.openSearch();
                break;
            case R.id.downloaded:
                Snackbar.make(findViewById(R.id.fab),"显示下载文件",Snackbar.LENGTH_SHORT).show();
                break;
            default:
        }
        return true;
    }
}
